<?php
/**
 * @brief 升级更新控制器
 */
class Update extends IController
{
	/**
	 * @brief 升级更新
	 */
	public function index()
	{
		set_time_limit(0);

		$sql = array(
		    "ALTER TABLE `{pre}goods` ADD `config` text COMMENT '商品配置参数'",

            "DROP TABLE IF EXISTS `{pre}goods_extend_preorder_discount`;",
            "CREATE TABLE `{pre}goods_extend_preorder_discount` (
              `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
              `goods_id` int(11) unsigned NOT NULL COMMENT '商品ID',
              `product_id` int(11) unsigned default '0' COMMENT '货品ID',
              `start_date` date NOT NULL COMMENT '服务开始日期',
              `end_date` date NOT NULL COMMENT '服务结束日期',
              `discount` decimal(15,2) NOT NULL COMMENT '浮动价格正负数',
              `seller_id` int(11) unsigned DEFAULT '0' COMMENT '商家ID',
              PRIMARY KEY (`id`),
              index (`goods_id`),
              index (`product_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='时间预订类商品价格浮动表';",


            "DROP TABLE IF EXISTS `{pre}goods_extend_preorder_disnums`;",
            "CREATE TABLE `{pre}goods_extend_preorder_disnums` (
              `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
              `goods_id` int(11) unsigned NOT NULL COMMENT '商品ID',
              `product_id` int(11) unsigned NOT NULL default '0' COMMENT '货品ID',
              `start_date` date NOT NULL COMMENT '服务开始日期',
              `end_date` date NOT NULL COMMENT '服务结束日期',
              `disnums` int(11) NOT NULL COMMENT '浮动库存正负数',
              `seller_id` int(11) unsigned DEFAULT '0' COMMENT '商家ID',
              PRIMARY KEY (`id`),
              index (`goods_id`),
              index (`product_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='时间预订类商品库存浮动表';",


            "DROP TABLE IF EXISTS `{pre}order_extend_preorder`;",
            "CREATE TABLE `{pre}order_extend_preorder` (
              `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
              `order_id` int(11) unsigned NOT NULL COMMENT '订单ID',
              `goods_id` int(11) unsigned NOT NULL COMMENT '商品ID',
              `product_id` int(11) unsigned NOT NULL default '0' COMMENT '货品ID',
              `goods_nums` int(11) unsigned NOT NULL default '1' COMMENT '商品数量',
              `start_date` date NOT NULL COMMENT '服务开始日期',
              `end_date` date NOT NULL COMMENT '服务结束日期',
              PRIMARY KEY (`id`),
              index (`order_id`),
              index (`goods_id`),
              index (`product_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='订单扩展表预订类';",
		);

		foreach($sql as $key => $val)
		{
		    IDBFactory::getDB()->query( $this->_c($val) );
		}

		//把plugin表里面输入写入到config/plugin_config.php里面
		$pluginDB = new IModel('plugin');
		$pluginData = $pluginDB->query();
		$insertData = [];
		foreach($pluginData as $key => $item)
		{
		    $insertData[$item['class_name']] = ['is_open' => $item['is_open'],'config_param' => $item['config_param']];
		}
		Config::edit('config/plugin_config.php',$insertData);

        //清空runtime缓存
		$runtimePath = IWeb::$app->getBasePath().'runtime';
		$result      = IFile::clearDir($runtimePath);
		die("升级成功!! V5.6版本");
	}

	public function _c($sql)
	{
		return str_replace('{pre}',IWeb::$app->config['DB']['tablePre'],$sql);
	}
}